package com.acoffee.maven.controller;

import com.acoffee.maven.bean.UserVo;
import com.acoffee.maven.pojo.Dept;
import com.acoffee.maven.pojo.User;
import com.acoffee.maven.service.DeptService;
import com.acoffee.maven.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomBooleanEditor;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author acoffee
 * @create 2021-05-14 15:38
 */
@Controller
@RequestMapping(value = "/user")
public class UserController extends BaseController{

//    //如果我们将@InitBinder配置在UserController中就只对UserController有效
//    @InitBinder
//    public void initBinder(WebDataBinder webDataBinder){
//        webDataBinder.registerCustomEditor(Date.class,new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
//    }

    private Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    private UserService userService;
    @Autowired
    private DeptService deptService;

    /**
     * 控制器中的处理方法的返回值为跳转路径
     * 此功能讲解从前台传递对象到控制器
     * 如果使用的是对象的传值，那些只要前台的表单的name的值和对象中的属性名的值相同就可以接受到数据
     */
    @RequestMapping(value = "/list")
    public String list(UserVo userVo, HttpServletRequest request) {
        List<User> userList = userService.findUserByPage(userVo);
        request.setAttribute("userlist", userList);//转发设置到requset域中
        request.setAttribute("uv", userVo);
        return "/user/list.jsp";
    }

    @RequestMapping("/goAddUser")
    public ModelAndView goAddUser() {
        ModelAndView modelAndView = new ModelAndView();
        List<Dept> deptList = deptService.findAllDepts();
        //通过ModelAndView存放的数据全部是放在request域对象中的
        modelAndView.addObject("deptList", deptList);
        modelAndView.setViewName("/user/add.jsp");
        return modelAndView;
    }
    @RequestMapping(value = "/addUser")
    public String addUser(User user,
                          @RequestParam("file") MultipartFile file,
                          HttpServletRequest request){
        //获取服务器上路径，这里我们将图片传至服务器的指定文件夹，然后再从指定文件夹获取
        String serverImageDirPath = request.getServletContext().getRealPath("/static/images");
        logger.info("服务器上的图片文件夹的路径："+serverImageDirPath);
        String imagePath = serverImageDirPath+"/"+file.getOriginalFilename();
        File uploadImage = new File(imagePath);
        try {
            file.transferTo(uploadImage);
            user.setHeadimg(file.getOriginalFilename());
            this.userService.addUser(user);
        } catch (IOException e) {
            e.printStackTrace();
        }
        this.userService.addUser(user);
        return "list";
    }
}
